/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccess;
import Adapter.DomainAdapter;
import be.ac.ulg.montefiore.run.jahmm.*;
import java.io.FileWriter;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import Domain.*;
/**
 *
 * @author Allen
 */
public class HmmWriter {

    public static void writeHmms
            (String filename, List<Hmm<ObservationDiscrete<Output>>> hmm_list){
        try {
            FileOutputStream fs = new FileOutputStream(filename);
            PrintStream p = new PrintStream(fs);

            for(int i = 0; i < hmm_list.size(); i ++) {
                writeHmm(p, hmm_list.get(i));
                p.println(";#" + i);
            }
            p.close();
            fs.close();

        } catch (IOException e){

        }
            
    }

    public static void writeHmm(PrintStream p, Hmm<ObservationDiscrete<Output>> hmm){
        double T[][] = new double[3][3];
        double O[][] = new double[3][3];
        DomainAdapter.toMatrix(hmm, T, O);

        p.print("T=");
        writeMatrix(p, T);
        p.print(";");

        p.print("O=");
        writeMatrix(p, O);
        p.print(";");
    }
    
    public static void writeMatrix(PrintStream p, double[][] M){
        
        java.text.DecimalFormat df = new java.text.DecimalFormat("#0.000");
        for(int j = 0; j < 3; j ++) {
            p.print("{");
            for(int k = 0; k < 3; k ++) {
                p.print(""+df.format(M[j][k])+", ");
            }
            p.print("},");
        }
        p.print("}");
    }
}
